package com.haozi.ehub.user.config;

import com.haozi.ehub.core.util.CodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * 目前为止并无暖用的跨域处理
 * @author ：terry
 * @date ：Created in 2020/7/2 14:21
 * @description：解决跨域问题
 * @version: 1.0
 */
@Slf4j
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        log.info("Into CorsConfig class corsWebFilter method");
        CorsConfiguration configuration = new CorsConfiguration();
        final String all = CodeUtils.ALL;
        //配置跨域
        configuration.addAllowedMethod(all);
        configuration.addAllowedOrigin(all);
        configuration.addAllowedHeader(all);

        //配置允许的方法
        configuration.addAllowedMethod(CodeUtils.GET_U);
        configuration.addAllowedMethod(CodeUtils.POST_U);
        configuration.addAllowedMethod(CodeUtils.PUT_U);
        configuration.addAllowedMethod(CodeUtils.DELETE_U);
        configuration.addAllowedMethod(CodeUtils.HEAD_U);
        configuration.addAllowedMethod(CodeUtils.PATCH_U);
        configuration.addAllowedMethod(CodeUtils.OPTIONS_U);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration(CodeUtils.URL_ALL, configuration);

        log.info("Execute CorsConfig class corsWebFilter method");
        return new CorsWebFilter(source);
    }
}
